package state

import (
	"errors"
	"fmt"
)

type tWritingState struct {
	context iFileStreamContext
	written int
}


func newWritingState(context iFileStreamContext) iFileStreamState {
	return &tWritingState{
		context,
		0,
	}
}

func (me *tWritingState) OpenRead() error {
	return errors.New(fmt.Sprintf("tWritingState.OpenRead, already writing %s", me.context.File()))
}

func (me *tWritingState) OpenWrite() error {
	return errors.New(fmt.Sprintf("tWritingState.OpenWrite, already writing %s", me.context.File()))
}

func (me *tWritingState) Read(p []byte) (n int, err error) {
	return 0, errors.New(fmt.Sprintf("tWritingState.Read, cannot read %s", me.context.File()))
}

func (me *tWritingState) Write(p []byte) (n int, err error) {
	size := len(p)
	me.written += size
	fmt.Printf("tWritingState.Write, file=%s, written=%v\n", me.context.File(), me.written)
	return size, nil
}

func (me *tWritingState) Close() error {
	fmt.Printf("tWritingState.Close, file=%s, written=%v\n", me.context.File(), me.written)
	me.context.Switch(newClosedState(me.context))
	return nil
}